let going = true;
import { isEncryption } from './baseUrl';
export default (method = '', data = '') => {
  return new Promise((resolve, reject) => {
    let _data = data;
    if (method == 'goPath' || method == 'showPwdModel') {
      if (going) {
        if (
          method == 'goPath' &&
          _data &&
          _data.type == 'webView' &&
          _data.param &&
          _data.param.url
        ) {
          _data.param.url =
            (_data.param.url.match(/(http|https):\/\/([\w.]+\/?)\S*/) ? '' : location.origin) +
            _data.param.url;
        }
        going = false;
        setTimeout(() => {
          going = true;
        }, 2000);
      } else {
        return;
      }
    }
    _data = typeof _data === 'object' ? JSON.stringify(_data) : _data || '';
    if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
      var bridgeInit = () => {
        return new Promise((_resolve, _reject) => {
          if (window.WebViewJavascriptBridge) {
            _resolve();
          } else {
            var WVJBIframe = document.createElement('iframe');
            WVJBIframe.style.display = 'none';
            WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
            document.documentElement.appendChild(WVJBIframe);
            setTimeout(function () {
              document.documentElement.removeChild(WVJBIframe);
              _resolve();
            }, 500);
          }
        });
      };
      bridgeInit().then(res => {
        if (
          method &&
          window.WebViewJavascriptBridge &&
          window.WebViewJavascriptBridge.callHandler
        ) {
          window.WebViewJavascriptBridge.callHandler(method, _data, function (response) {
            var resp = method == 'getEncrypt' ? response : JSON.parse(response);

            resolve(response ? resp : '');
          });
        } else {
          // reject("请在App端打开");
        }
      });
    } else {
      // if (method && window.android && window.android[method]) {
      if (method && window.android) {
        var res = '';
        if (method == 'showPwdModel') {
          window.androidResolveCheck = check => {
            if (
              !isEncryption &&
              typeof check == 'object' &&
              typeof check['encryption'] == 'string'
            ) {
              check['encryption'] = JSON.parse(check['encryption']);
            }
            resolve(check);
          };
          window.android[method](_data);
        } else {
          if (_data) {
            res =
              method == 'getEncrypt' && isEncryption
                ? window.android[method](_data)
                : JSON.parse(window.android[method](_data));
          } else {
            res = JSON.parse(window.android[method]());
          }
          if (res && method == 'getToken' && res['token']) {
            window.sessionStorage.setItem('token', res['token']);
            window.sessionStorage.setItem('sign', res['sign']);
          }
          resolve(res);
        }
      } else {
        resolve();
      }
    }
  });
};
